<?php

require_once "../common/curl.php";

/*
function LoadCountries()
{
	$xml = simplexml_load_file("http://api.erepublik.com/v2/feeds/countries");
	
	foreach ($xml->country as $country) {
		echo $country->id . "," . $country->name . "\r\n";
	} 
}
*/

function ParseOne($id)
{
	global $myCURL;

	$file = $myCURL->get("http://api.erepublik.com/v2/feeds/market/14/$id");
	$xml = new SimpleXMLElement($file);	

	$result = '';
	foreach ($xml->offer as $offer) {
		$result .= sprintf("price=%s, amount=%s\r\n", $offer->price, $offer->amount);
	}
	
	echo $result;
	return $result;
}


// main 

$countries = file('countries.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$myCURL = new cUrl();
$content = '';

foreach ($countries as $country) {
	$a = explode(',', trim($country));
	echo "Parsing $a[1]:\r\n";
	$content .= "$a[1]:\r\n";
	$content .= ParseOne($a[0]);
	echo "------------\r\n";
	$content .= "------------\r\n";
}

$file = fopen('iron.txt', 'w');
fwrite($file, $content);
fclose($file);

?>
